import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XML_Parser {

	public static void main(String[] args) 
			throws ParserConfigurationException, SAXException, IOException {
		
		//printXMLTree();
		printXMLFile();
	}

	
	
	private static void printXMLFile() 
    		throws ParserConfigurationException, SAXException, IOException
    {  	
    	String xml="soapresponse.xml";
    	File fXmlFile = new File(xml);
    	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    	Document doc = dBuilder.parse(fXmlFile);
     
    	doc.getDocumentElement().normalize();
     
    	//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
       
    	NodeList nodeList = doc.getElementsByTagName("GetCityWeatherByZIPResult");
    	//NodeList nodeList = doc.getElementsByTagName("*");

    	System.out.println("----------------------------");
        for (int i = 0; i < nodeList.getLength(); i++)
        {
            Node node = nodeList.item(i);
    		if (node.getNodeType() == Node.ELEMENT_NODE) 
    		{
    			    			
    			Element eElement = (Element) node;    
     
    			System.out.println("Success : " + eElement.getElementsByTagName("Success").item(i).getTextContent());
    			System.out.println("ResponseText : " + eElement.getElementsByTagName("ResponseText").item(i).getTextContent());
    			System.out.println("State : " + eElement.getElementsByTagName("State").item(i).getTextContent());
    			System.out.println("City : " + eElement.getElementsByTagName("City").item(i).getTextContent());
    			System.out.println("WeatherStationCity : " + eElement.getElementsByTagName("WeatherStationCity").item(i).getTextContent());
    			System.out.println("WeatherID : " + eElement.getElementsByTagName("WeatherID").item(i).getTextContent());
    			System.out.println("Description : " + eElement.getElementsByTagName("Description").item(i).getTextContent());
    			System.out.println("Temperature : " + eElement.getElementsByTagName("Temperature").item(i).getTextContent());
    			System.out.println("RelativeHumidity : " + eElement.getElementsByTagName("RelativeHumidity").item(i).getTextContent());
    			System.out.println("Wind : " + eElement.getElementsByTagName("Wind").item(i).getTextContent());
    			System.out.println("Pressure : " + eElement.getElementsByTagName("Pressure").item(i).getTextContent());
    			System.out.print("Visibility : "); 
    			if(eElement.getElementsByTagName("Visibility").item(i).getTextContent()!=null)
    				{
    					System.out.println(eElement.getElementsByTagName("Visibility").item(i).getTextContent());
    				}
    			System.out.print("Windchill : ");
    			if(eElement.getElementsByTagName("WindChill").item(i).getTextContent()!=null)
    			{
    				System.out.println(eElement.getElementsByTagName("WindChill").item(i).getTextContent());
    			}
    			System.out.print("Remarks : ");
    			if(eElement.getElementsByTagName("Remarks").item(i).getTextContent()!=null)
    			{
    				System.out.println(eElement.getElementsByTagName("Remarks").item(i).getTextContent());
    			}

    		}
        
        }
    		  
    		
    }
       
    
    private static void printXMLTree() 
    		throws ParserConfigurationException, SAXException, IOException
    {  	
    	String xml="soapresponse.xml";
    	File fXmlFile = new File(xml);
    	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    	Document doc = dBuilder.parse(fXmlFile);
     
    	doc.getDocumentElement().normalize();
     
    	//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
      

    	//NodeList nodes = doc.getElementsByTagName("GetCityWeatherByZIPResult");
    	NodeList nodes = doc.getElementsByTagName("*");

    	
    	for (int i = 0; i < nodes.getLength(); i++) 
    	{
    		Node node = nodes.item(i);
    		//System.out.print(i+"---");	
            System.out.println(node.getNodeName()+": "+node.getTextContent());               

    	  
    		if(node.hasChildNodes()==true)
    		{
    			System.out.println(node.getNodeName());
    			
    			Node child = node.getFirstChild();
        		while(node.getNextSibling()!=null)
        		{
                    if (child.getNodeType() == Node.ELEMENT_NODE) 
                    {
                       System.out.println(child.getNodeName()+": "+child.getTextContent());               
                    }

        		}
    		}
    		
    		
    	
    	}
    		  
    		
    }
       

	
	
	
}
